<?php

class InfoAction extends CommonAction {
    
    // public function index() {
    //     // $arr=$m->order('create_time desc')->select();
    //     $this->display();
    // }
    public function index()
    {
        $name = $this->getActionName();
        $m = M($name); // 实例化Data数据对象
        import('ORG.Util.Page');// 导入分页类
        //是否为全站总管理员
        if(session('group') != 1)$map = array('aid'=>session('areaid'));
        //搜索条件
        $search_str=$this->_get('search_str');
        if(!empty($search_str))$map['title']=array('like','%'.$search_str.'%');
        $count      = $m->where($map)->count();
        $Page       = new Page($count,50);
        $Page->rollPage = 20;   //最多显示20个分页

        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数, 使用 $_GET[p]获取
        $nowPage = isset($_REQUEST['p']) ? $_REQUEST['p'] : 1;
        $offect = ($nowPage-1)*$Page->listRows;
        $list = $m->where($map)->order('id desc')->limit($offect,$Page->listRows)->select();
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('search_str',$search_str);
        $this->assign('list',$list);
        $this->display();
    }

    public function edit() {
        $m = M('Info');
        $this->assign('group', session('group'));
        if($_POST['subflag']){
            $m->create();
            $m->create_time = !empty($_POST['create_time'])?$_POST['create_time']:date('Y-m-d H:i:s');
            $m->aid = ($_POST[aid]==-1)?$_POST[city]:$_POST[aid];
            $result = $m->save();

            if($result) {
                $this->success('操作成功！',"index");
            }else{
                $this->error('写入错误！',"index");
            }
           
        }else{        
            if ($this->_get('id')) {
                $condition['id'] = $this->_get('id');
                if(session('group') != 1)$condition['aid'] =session('areaid');
                $res = $m->where($condition)->select();

                if($res){
                    //信息分类
                    $type = getCategorys(13);
                    $this->assign('type', $type); 
                    //地区
                    $aresult = getAreaList($res[0][aid]);
                    $this->assign('aresult', $aresult);

                    $this->assign('vo', $res[0]); 
                }
            }else{
                $this->ajaxReturn(0, '非法进入', 0);
            }

            $this->display();
        }
    }
  

    
    
    public function add(){
        
        if($_POST['subflag']){
            $m=M('Info');
            $m->create();
            $m->create_time = !empty($_POST['create_time'])?$_POST['create_time']:date('Y-m-d H:i:s');
            $m->aid = ($_POST[aid]==-1)?$_POST[city]:$_POST[aid];

            if(session('group') != 1)$m->aid =session('areaid');
            $result = $m->add();
            if($result) {
                $this->success('操作成功！',"index");
                exit(0);
            }else{
                $this->error('写入错误！',"index");
                exit(0);
            }
        }

        //新闻分类
        $type = getCategorys(13);
        $this->assign('type', $type); 
        //邯郸各个县
        $area = getAreas();
        $this->assign('area', $area);
        $this->assign('group', session('group'));
        $this->display();
    }
    
    public function show() {
        
        if ($this->_get('id')) {
            $m = M('Info');
            $condition['id'] = $this->_get('id');
            $res = $m->where($condition)->select();
            $catname =  showCategory($res[0]['catid']);
            $areaname =  showArea($res[0]['aid']);
            if($res){
                $this->assign('vo', $res[0]); 
                $this->assign('catname', $catname); 
                $this->assign('areaname', $areaname); 
            }
        }else{
            $this->ajaxReturn(0, '非法进入', 0);
        }


        $this->display();
    }

    public function remove()
    {
        //删除指定记录
        $result = array('isErr'=>0,'content'=>'');
        $id = $_REQUEST['id'];

        if(!empty($id))
        {
            $m = M('Info');
            $pk = $m->getPk ();
            $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
            $ids = explode (',',$id);
            if(session('group') != 1)$condition['aid']=session('areaid');
            foreach($ids as $aid)
            {
                $condition['id'] = $aid;
                $res = $m->where($condition)->delete();
            }

        }
        else
        {
            $result['isErr'] = 1;
            $result['content'] = L('ACCESS_DENIED');
        }

        die(json_encode($result));
    }
    public function setStatus()
    {
        //删除指定记录
        $result = array('isErr'=>0,'content'=>'');
        $id = $_REQUEST['id'];

        if(!empty($id))
        {
            $m = M('Info');
            if(session('group') != 1)$condition['aid']=session('areaid');
        
            $condition['id'] = $id;
            $res = $m->find($id);
            if($res['status']==0)$data['status'] = 1;
            elseif($res['status']==1)$data['status'] = 0;
            $m->where($condition)->save($data);
        }
        else
        {
            $result['isErr'] = 1;
            $result['content'] = L('ACCESS_DENIED');
        }

        die(json_encode($result));
    }

    public function setRecommend()
    {
        //删除指定记录
        $result = array('isErr'=>0,'content'=>'');
        $id = $_REQUEST['id'];

        if(!empty($id))
        {
            $m = M('Info');
            if(session('group') != 1)$condition['aid']=session('areaid');
        
            $condition['id'] = $id;
            $res = $m->find($id);
            if($res['recommend']==0)$data['recommend'] = 1;
            elseif($res['recommend']==1)$data['recommend'] = 0;
            $m->where($condition)->save($data);
        }
        else
        {
            $result['isErr'] = 1;
            $result['content'] = L('ACCESS_DENIED');
        }

        die(json_encode($result));
    }
}